﻿using CMS.Models;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace CMS.ViewModel
{
    public class AddCargoTypeViewModel:ViewModelBase
    {
        public AppData AppData { get; set; }=AppData.Instance;
        private CargoType cargoType=new CargoType();
        /// <summary>
        /// 当前物资类型
        /// </summary>
        public CargoType CargoType
            
        {
            get { return cargoType; }
            set { cargoType = value; RaisePropertyChanged(nameof(CargoType));  }
        }
        /// <summary>
        /// 关闭窗体
        /// </summary>
        public RelayCommand<Window> AddCargoTypeCloseCommand
        {
            get
            {
                return new RelayCommand<Window>((arg) =>
                {
                    if (arg == null) return;
                    arg.Close();
                });
            }
        }
        /// <summary>
        /// 添加新的物资类型
        /// </summary>
        public RelayCommand<Window> AddCargoTypeCommand
        {
            get
            {
                return new RelayCommand<Window>((arg) =>
                {
                    if (string.IsNullOrEmpty(cargoType.Name)) return;
                    cargoType.InserDate=DateTime.Now;
                    cargoType.MenberId=AppData.CurrentUser.Id;
                    cargoType.MenberName = AppData.CurrentUser.Name;
                    var count = new CargoTypeProvider().Insert(cargoType);
                    if (count == 0)
                    {
                        MessageBox.Show("添加失败");
                    }
                    else{
                        arg.Close();
                    }
                });
            }
        }
    }
}
